package com.sanjiahantang.youjiarole.module.complain.views.impl;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.complain.presenters.ComplainPresenter;
import com.sanjiahantang.youjiarole.module.complain.views.ComplainView;
import com.sanjiahantang.youjiarole.module.decorate.beans.DecorateManageBean;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 可选被投诉人列表
 * Created by zy on 2017/11/28.
 */

public class ComplainRoleListActivity extends BaseMvpActivity<ComplainView,ComplainPresenter> implements ComplainView, View.OnClickListener {

    private DecorateManageBean.Data decorateData;

    private String[] roleName = new String[]{
            "客户",
            "项目经理",
            "监理"
    };

    private int[] roleType = new int[]{
            1,
            2,
            3
    };

    private String currentPhone;


    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public ComplainPresenter initPresenter() {
        return new ComplainPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_common_list);

        TextView title = (TextView) findViewById(R.id.title);
        title.setText("投诉人员");

        findViewById(R.id.fl_Left).setOnClickListener(this);
        decorateData = (DecorateManageBean.Data) getIntent().getSerializableExtra(KEY_PARAM);


//        ListView listView = (ListView) findViewById(R.id.listView);
//        listView.setAdapter(new QuickAdapter<RoleList>() {
//        });

        currentPhone = SPUtils.get($this,SPConfig.PHONE,"").toString();
    }

    @Override
    public void initData() {
        QuickAdapter<DecorateManageBean.Role> participantAdapter;
        ListView listView = (ListView) findViewById(R.id.listView);
        HashMap<String,DecorateManageBean.Role> roleHashMap = decorateData.getRoleList().get(0);
        List<DecorateManageBean.Role> list = new ArrayList<>();
        String designerPhone = decorateData.getRoleDesignerPhone();
        final String projectPhone = decorateData.getRoleProjectPhone();
        final String supervisionPhone = decorateData.getRoleSupervisionPhone();

        final String nowPhone = SPUtils.get($this, SPConfig.PHONE,"").toString();
        DecorateManageBean.Role customerRole = new DecorateManageBean.Role();
        customerRole.setType(1);
        customerRole.setUserName(decorateData.getCustomerName());
        customerRole.setPhone(decorateData.getCustomerPhone());
        list.add(customerRole);
        if (designerPhone != null && roleHashMap.containsKey("designer")){
            DecorateManageBean.Role engineerRole = null;
            if (roleHashMap.containsKey("engniee")){
                engineerRole = roleHashMap.get("engniee");
            }else if (designerPhone.equals(projectPhone)){
                engineerRole = roleHashMap.get("designer");
            }

            if (engineerRole != null){
                engineerRole.setType(2);
                if (decorateData.getIsSubcontractProject() == 1){
                    if (!nowPhone.equals(projectPhone) && !nowPhone.equals(decorateData.getProjectPhone())){
                        list.add(engineerRole);
                    }
                }else{
                    if (!nowPhone.equals(projectPhone)){
                        list.add(engineerRole);
                    }
                }

            }

            DecorateManageBean.Role supervisionRole = null;
            if (roleHashMap.containsKey("supervision")){
                supervisionRole = roleHashMap.get("supervision");
            }else if (designerPhone.equals(supervisionPhone)){
                supervisionRole = roleHashMap.get("designer");
            }

            if (supervisionRole != null){
                supervisionRole.setType(3);
                if (decorateData.getIsSubcontractProject() == 1){
                    if (!supervisionPhone.equals(projectPhone) && !currentPhone.equals(supervisionPhone)){
                        list.add(supervisionRole);
                    }
                }else{
                    if (!nowPhone.equals(supervisionPhone)){
                        list.add(supervisionRole);
                    }
                }

            }
        }

        participantAdapter = new QuickAdapter<DecorateManageBean.Role>($this,R.layout.item_role,list) {
            @Override
            protected void convert(BaseAdapterHelper helper, final DecorateManageBean.Role item) {
                ImageView ivPhoto = helper.getView(R.id.iv_image);
                Picasso.with(context).load(FileUtils.rePath(item.getUrl()))
                        .placeholder(R.drawable.i_head_default)
                        .into(ivPhoto);
                helper.getView().setBackgroundColor(Color.WHITE);
                helper.setText(R.id.name,item.getUserName());
                helper.setText(R.id.phone,item.getPhone());

                final Bundle bundle=new Bundle();
                bundle.putSerializable(KEY_OBJECT,item);
                bundle.putInt(KEY_ROLE_CODE,item.getType());

                helper.setText(R.id.time,roleName[item.getType() - 1]);

                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setResult(RESULT_OK,new Intent().putExtra(KEY_PARAM,bundle));
                        finish();
                    }
                });
            }
        };

        listView.setAdapter(participantAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
